package com.starleague.hexagonal.android;

import java.util.Collection;

import android.graphics.Canvas;
import android.graphics.Rect;

import com.starleague.hexagonal.HexOutOfBoundsException;
import com.starleague.hexagonal.IHex;
import com.starleague.hexagonal.IHexCoordinate;
import com.starleague.hexagonal.IHexGrid;
import com.starleague.hexagonal.ImmutableHexCoordinate;
import com.starleague.hexagonal.flat.FlatHexGridUtil;

public class HexGridPainter
{
    private final IHexGrid hexGrid;
    private final IHexPainter hexPainter;
    private final IHexGridMetrics hexGridMetrics;

    public HexGridPainter(IHexGrid hexGrid, IHexGridMetrics hexGridMetrics, IHexPainter hexPainter)
    {
        this.hexGrid = hexGrid;
        this.hexPainter = hexPainter;
        this.hexGridMetrics = hexGridMetrics;
    }

    private void paintHexAt(Canvas canvas,
                            Rect drawingRect,
                            IHexCoordinate hexCoordinate,
                            IHex selectedHex,
                            Collection<? extends IHex> hilightedHexes) throws HexOutOfBoundsException
    {
        int hexLeftX = this.hexGridMetrics.getHexLeft(hexCoordinate);
        int hexTopY = this.hexGridMetrics.getHexTop(hexCoordinate);

        if ((hexLeftX + this.hexGridMetrics.getHexWidth() > drawingRect.left + 20)
            && (hexTopY + this.hexGridMetrics.getHexHeight() > drawingRect.top + 20)
            && (hexLeftX < drawingRect.right - 20)
            && (hexTopY < drawingRect.bottom - 20))
        {
            IHex hex = this.hexGrid.getHexAt(hexCoordinate);
            boolean isSelected = (hex == selectedHex);
            boolean isHilighted = (hilightedHexes != null) && hilightedHexes.contains(hex);
            this.hexPainter.paint(canvas, hexLeftX, hexTopY, hex, isSelected, isHilighted);
        }
    }

    public void paint(Canvas canvas,
                      Rect drawingRect,
                      IHex selectedHex,
                      Collection<? extends IHex> hilightedHexes) throws HexOutOfBoundsException
    {
        for (int gridY = 0; gridY < this.hexGrid.getNumHexesY(); ++gridY)
        {
            for (int gridX = 0; gridX < this.hexGrid.getNumHexesX(); ++gridX)
            {
                // TODO: Move FlatHex calculations elsewhere
                if (FlatHexGridUtil.isAboveSameGridY(gridX))
                {
                    IHexCoordinate hexCoordinate = new ImmutableHexCoordinate(gridX, gridY);
                    paintHexAt(canvas, drawingRect, hexCoordinate, selectedHex, hilightedHexes);
                }
            }
            for (int gridX = 0; gridX < this.hexGrid.getNumHexesX(); ++gridX)
            {
                // TODO: Move FlatHex calculations elsewhere
                if (!FlatHexGridUtil.isAboveSameGridY(gridX))
                {
                    IHexCoordinate hexCoordinate = new ImmutableHexCoordinate(gridX, gridY);
                    paintHexAt(canvas, drawingRect, hexCoordinate, selectedHex, hilightedHexes);
                }
            }
        }
    }
}
